<template>
	<view class="container">
		<!-- 运单基本信息卡片 -->
		<view class="card waybill-card">
			<view class="card-header">
				<text class="card-title">运单信息</text>
				<text class="waybill-status" :class="waybillInfo.status">{{ waybillInfo.status_text }}</text>
			</view>
			<view class="card-body" @click="toStubs(waybillInfo.id)">
				<view class="info-row">
					<text class="info-label">运单号</text>
					<text class="info-value" style="padding-right: 6rpx;">{{ waybillInfo.express_no }}</text>
					<uv-icon name="file-text" color="#999" size="18" @click="copy(waybillInfo.express_no)"></uv-icon>
				</view>
				<view class="info-row">
					<text class="info-label">发货人</text>
					<text class="info-value">{{ waybillInfo.send_name }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">收货人</text>
					<text class="info-value">{{ waybillInfo.receipt_name }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">联系电话</text>
					<text class="info-value">{{ waybillInfo.receipt_phone }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">收货地址</text>
					<text class="info-value address">{{ waybillInfo.full_receipt_address }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">货物信息</text>
					<text class="info-value">{{ waybillInfo.goods_name }}</text>
				</view>
			</view>
		</view>

		<!-- 物流详情 -->
		<view class="card logistics-card">
			<view class="card-header">
				<text class="card-title">物流详情</text>
			</view>
			<view class="logistics-body">
				<view class="timeline">
					<view v-for="(item, index) in logisticsList" :key="index" class="timeline-item"
						:class="{ 'first-item': index === 0, 'last-item': index === logisticsList.length - 1 }">
						<view class="timeline-dot"></view>
						<view class="timeline-line" v-if="index !== logisticsList.length - 1"></view>
						<view class="timeline-content">
							<view class="logistics-desc">{{ item.context }}</view>
							<view class="logistics-time">{{ item.time }}
							</view>
							<view class="logistics-location" v-if="item.location">
								<text class="location-icon">📍</text>
								{{ item.location }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				waybillInfo: {},
				logisticsList: []
			};
		},
		onLoad(option) {
			this.getWaybillDetail(option.id);
		},
		methods: {
			getWaybillDetail(id) {
				uni.$uv.http.post('/wanlshop.express/getExpressDetail', {
					order_id: id
				}).then(res => {
					let data = res.res.data;
					this.waybillInfo = data;
					this.logisticsList = data.track_list;
				}).catch(error => {
					this.$wanlshop.msg('获取失败，请稍后重试', 2000, true)
				});
			},
			// 跳转电子存根页面
			toStubs(id) {
				this.$wanlshop.to('/pages/express/stubs?id=' + id);
			},
			// 复制运单号
			copy(str) {
				let that = this;
				uni.setClipboardData({
					data: str,
					success: () => {
						that.$wanlshop.msg('运单号已复制');
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background-color: #f8f9fa;
		padding: 24rpx;
	}

	.card {
		background: #ffffff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.06);
		margin-bottom: 24rpx;
		overflow: hidden;

		.card-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 32rpx 32rpx 24rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.card-title {
				font-size: 32rpx;
				font-weight: 600;
				color: #333;
			}

			.waybill-status {
				font-size: 24rpx;
				padding: 8rpx 20rpx;
				border-radius: 20rpx;

				&.start {
					background: #fff0e6;
					color: #fe6600;
				}
			}
		}

		.card-body {
			padding: 24rpx 32rpx 32rpx;
		}
	}

	.info-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 28rpx;

		&:last-child {
			margin-bottom: 0;
		}

		.info-label {
			font-size: 28rpx;
			color: #666;
			width: 160rpx;
			flex-shrink: 0;
		}

		.info-value {
			flex: 1;
			font-size: 28rpx;
			color: #333;
			text-align: right;
			line-height: 1.5;

			&.address {
				word-break: break-all;
			}
		}
	}

	.logistics-body {
		padding: 0 32rpx 32rpx;
	}

	.timeline {
		position: relative;
		padding: 20rpx 0;
	}

	.timeline-item {
		display: flex;
		align-items: flex-start;
		position: relative;
		margin-bottom: 40rpx;

		&:last-child {
			margin-bottom: 0;
		}

		&.first-item .timeline-dot {
			background: #fe6600;
			border: 4rpx solid #ffe6d6;
			margin-top: 8rpx;
		}
	}

	.timeline-dot {
		width: 24rpx;
		height: 24rpx;
		border-radius: 50%;
		background: #ddd;
		flex-shrink: 0;
		margin-right: 24rpx;
		position: relative;
		z-index: 2;
	}

	.timeline-line {
		position: absolute;
		left: 12rpx;
		top: 24rpx;
		width: 2rpx;
		height: calc(100% + 40rpx);
		background: #e8e8e8;
		z-index: 1;
	}

	.timeline-content {
		flex: 1;
		padding-bottom: 8rpx;

		.logistics-desc {
			font-size: 30rpx;
			color: #333;
			line-height: 1.5;
			margin-bottom: 12rpx;
		}

		.logistics-time {
			font-size: 24rpx;
			color: #fe6600;
			margin-bottom: 8rpx;
		}

		.logistics-location {
			font-size: 24rpx;
			color: #999;
			display: flex;
			align-items: center;

			.location-icon {
				margin-right: 8rpx;
				font-size: 20rpx;
			}
		}
	}

	/* 第一个和最后一个时间轴项目的特殊样式 */
	.timeline-item.first-item .timeline-content .logistics-desc {
		font-weight: 600;
		color: #fe6600;
	}

	.timeline-item.last-item {
		margin-bottom: 0;
	}
</style>